%% US Public Debt and Safe Asset Market Power
%% Jason Choi, Rishabh Kirpalani, and Diego Perez
%% Nov 24, 2024

%% Solve Competitive Equilibrium w/ Alternate Benefit Function

%----------------------------------------------------------------
% 0. Housekeeping
%----------------------------------------------------------------

close all

%----------------------------------------------------------------
% 1. Defining variables
%----------------------------------------------------------------

// Endogenous Variables
var b rb rkstar rk krw_star kus_star krw kus kstar k wstar w c_rw c_us drdb spread vrw vus dMrwdb y;

// Exogenous Variables
var nnu oomega A Astar;

// Shocks
varexo eps_nnu eps_oomega eps_A;

// Paramters
parameters ggamma bbeta eeta llambda aalpha Astarbar Abar iiota iiota_star ddelta_rw ddelta_us
  nnu_bar oomega_bar rrho_nnu rrho_oomega ssigma_nnu ssigma_oomega rrho_A ssigma_A rrho_Astar ssigma_Astar
  b_nof rb_nof rkstar_nof rk_nof krw_star_nof kus_star_nof krw_nof kus_nof kstar_nof k_nof
  capKstar_nof capK_nof wstar_nof w_nof crw_nof cus_nof spread_nof vrw_nof vus_nof;

%----------------------------------------------------------------
% 2. Calibration
%----------------------------------------------------------------

% // Parameters
ggamma = 2;
bbeta = 0.9886;
eeta = 0.997;
llambda = 1;
aalpha = 0.3;
Astarbar = 0.9254;
Abar = 0.8154;
iiota = 0.9070;
iiota_star = 0.7939;
ddelta_rw = 0.1;
ddelta_us = 0.1;
nnu_bar = 2.1751;
oomega_bar = 0.0056;
rrho_nnu = 0.99;
ssigma_nnu = 0.01;
rrho_oomega = 0.95;
ssigma_oomega = 0.3;
rrho_A = 0.95;
ssigma_A = 0.02;
rrho_Astar = rrho_A;
ssigma_Astar = ssigma_A;

% Analytic Steady State (Monopoly Equilibrium)
% [bss] = fsolve(@(b) func_ireland(eeta,nnu_bar,llambda,oomega_bar,'ce',b),0.41);
% b_nof = bss;
b_nof = 0;
rb_nof = 1/bbeta - 1;
rkstar_nof = 1/bbeta + ddelta_rw - 1;
rk_nof = 1/bbeta + ddelta_us - 1;
krw_star_nof = ((aalpha*(1-iiota_star)*Astarbar*((1/bbeta+ddelta_rw-1)/(aalpha*iiota_star*Astarbar))^((aalpha*(1-iiota_star)-1)/(aalpha*(1-iiota_star))))/(1/bbeta+ddelta_us-1))^((aalpha*(1-iiota_star))/(1-aalpha));
kus_star_nof = ((1/bbeta+ddelta_rw-1)/(aalpha*iiota_star*Astarbar))^(1/(aalpha*(1-iiota_star)))*krw_star_nof^((1-iiota_star*aalpha)/(aalpha*(1-iiota_star)));
krw_nof = ((aalpha*iiota*Abar*((1/bbeta+ddelta_us-1)/(aalpha*(1-iiota)*Abar))^((aalpha*iiota-1)/(aalpha*iiota)))/(1/bbeta+ddelta_rw-1))^((aalpha*iiota)/(1-aalpha));
kus_nof = ((1/bbeta+ddelta_us-1)/(aalpha*(1-iiota)*Abar))^(1/(aalpha*iiota))*krw_nof^((1-(1-iiota)*aalpha)/(aalpha*iiota));
kstar_nof = krw_star_nof + krw_nof;
k_nof = kus_star_nof + kus_nof;
capKstar_nof = krw_star_nof^iiota_star*kus_star_nof^(1-iiota_star);
capK_nof = krw_nof^(1-iiota)*kus_nof^iiota;
wstar_nof = Astarbar*(1-aalpha)*(capKstar_nof)^aalpha;
w_nof = Abar*(1-aalpha)*(capK_nof)^aalpha;
crw_nof = wstar_nof + (rkstar_nof-ddelta_rw)*kstar_nof + rb_nof*b_nof;
cus_nof = w_nof + (rk_nof-ddelta_us)*k_nof - oomega_bar/(1+llambda)*(b_nof)^(1+llambda) - rb_nof*(b_nof);
drdb_nof = 0;
dMrwdb_nof = 0;
nnu_nof = nnu_bar;
oomega_nof = oomega_bar;
A_nof = Abar;
Astar_nof = Astarbar;
spread_nof = (rk_nof-ddelta_us-rb_nof);
vrw_nof = crw_nof^(1-ggamma)/(1-ggamma)/(1-bbeta);
vus_nof = cus_nof^(1-ggamma)/(1-ggamma)/(1-bbeta);
y_nof = A_nof*(kus_nof^iiota*krw_nof^(1-iiota))^aalpha;

%----------------------------------------------------------------
% 3. Model
%----------------------------------------------------------------

model;

c_rw^(-ggamma) = bbeta*c_rw(+1)^(-ggamma)*(1+rb);
c_rw^(-ggamma) = bbeta*c_rw(+1)^(-ggamma)*(1-ddelta_rw+rkstar);
c_rw + kstar + b = wstar + (1-ddelta_rw+rkstar(-1))*kstar(-1) + (1+rb(-1))*b(-1);

c_us^(-ggamma) = bbeta*c_us(+1)^(-ggamma)*(oomega*b^llambda+1+rb+drdb*b);
c_us^(-ggamma) = bbeta*c_us(+1)^(-ggamma)*(1-ddelta_us+rk);
c_us + k - b = w + (1-ddelta_us+rk(-1))*k(-1) - oomega(-1)/(1+llambda)*(b(-1))^(1+llambda) - (1+rb(-1))*b(-1);

rk = Astar*aalpha*(1-iiota_star)*krw_star^(aalpha*iiota_star)*kus_star^(aalpha*(1-iiota_star)-1);
rkstar = Astar*aalpha*iiota_star*krw_star^(aalpha*iiota_star-1)*kus_star^(aalpha*(1-iiota_star));
rk = A*aalpha*iiota*krw^(aalpha*(1-iiota))*kus^(aalpha*iiota-1);
rkstar = A*aalpha*(1-iiota)*krw^(aalpha*(1-iiota)-1)*kus^(aalpha*iiota);
wstar = Astar*(1-aalpha)*krw_star^(aalpha*iiota_star)*kus_star^(aalpha*(1-iiota_star));
w = A*(1-aalpha)*krw^(aalpha*(1-iiota))*kus^(aalpha*iiota);

drdb = 0;
dMrwdb = 0;

k = kus + kus_star;
kstar = krw + krw_star;

log(nnu) = (1-rrho_nnu)*log(nnu_bar) + rrho_nnu*log(nnu(-1)) + ssigma_nnu*eps_nnu;
log(oomega) = (1-rrho_oomega)*log(oomega_bar) + rrho_oomega*log(oomega(-1)) + ssigma_oomega*eps_oomega;
log(A) = (1-rrho_A)*log(Abar) + rrho_A*log(A(-1)) + ssigma_A*eps_A;
log(Astar) = (1-rrho_Astar)*log(Astarbar) + rrho_Astar*log(Astar(-1)) + ssigma_Astar*eps_A;

spread = (rk-ddelta_us-rb);

vrw = c_rw^(1-ggamma)/(1-ggamma) + bbeta*vrw(+1);
vus = c_us^(1-ggamma)/(1-ggamma) + bbeta*vus(+1);

y = A*(kus^iiota*krw^(1-iiota))^aalpha;

end;

%----------------------------------------------------------------
% 4. Computation
%----------------------------------------------------------------

initval;
  b = b_nof;
  rb = rb_nof;
  rkstar = rkstar_nof;
  rk = rk_nof;
  krw_star = krw_star_nof;
  kus_star = kus_star_nof;
  krw = krw_nof;
  kus = kus_nof;
  kstar = kstar_nof;
  k = k_nof;
  wstar = wstar_nof;
  w = w_nof;
  c_rw = crw_nof;
  c_us = cus_nof;
  drdb = drdb_nof;
  dMrwdb = dMrwdb_nof;
  nnu = nnu_nof;
  oomega = oomega_nof;
  spread = spread_nof;
  A = A_nof;
  Astar = Astar_nof;
  vrw = vrw_nof;
  vus = vus_nof;
  y = y_nof;
end;

resid;
check;

shocks;
    var eps_nnu = 1;
    var eps_oomega = 1;
    var eps_A = 1;
end;

set_dynare_seed('default');
stoch_simul(order=2,nograph,noprint,periods=100000,drop=1000,pruning);

%----------------------------------------------------------------
% 5. Generate moments
%----------------------------------------------------------------

spread_path = (rk-ddelta_us-rb)*100;
var_sp = var(spread_path);
auto_sp = autocorr(spread_path);
cost = oomega./(1+llambda).*(b).^(1+llambda);
benefit = (eeta-1).*b.*(log(b)-nnu);
var_by = var(b./y);
auto_by = autocorr(b./y);
corr_pq_by = corr(spread_path,b./y);

moments = [mean(b./y) mean(spread_path) var_by var_sp corr_pq_by auto_by(2) auto_sp(2)]';
data_mom = [0.41 0.62 0.03 0.086 -0.56 0.96 0.70]';
rowNames = {'Mean b/y','Mean sp','Var b/y','Var sp','Corr (b/y,sp)','Autocorr b/y','Autocorr sp'};
colNames = {'Model Moments','Data Moments'};
TableA0 = array2table([moments data_mom],'RowNames',rowNames,'VariableNames',colNames)

%----------------------------------------------------------------
% 6. Calculate welfare from transition
%---------------------------------------------------------------

oo_nof_alt = oo_;
M_nof_alt = M_;
options_nof_alt = options_;  

save nof_alt_save oo_nof_alt M_nof_alt options_nof_alt;

load me_alt_save;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulated 2nd Order Welfare
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Stochastic - lag issue fixed
NN = 100000;
BB = 5000;
NN_BB = NN+BB;

vrw_pos = strmatch('vrw',M_.endo_names,'exact');
vus_pos = strmatch('vus',M_.endo_names,'exact');

% Simulate ME
shock_matrix = randn(M_.exo_nbr,NN_BB);
sim_eq_me_ergo = simult_(M_me_alt,options_me_alt,oo_me_alt.dr.ys,oo_me_alt.dr,shock_matrix',options_me_alt.order);
sim_eq_me_ergo = sim_eq_me_ergo(:,BB+2:end); % burn

% ME to CE
shock_matrix = zeros(M_.exo_nbr,2);
for ii=1:NN
  sim_eq_me = simult_(M_me_alt,options_me_alt,sim_eq_me_ergo(:,ii),oo_me_alt.dr,shock_matrix',options_me_alt.order);
  vrw_sme(ii) = sim_eq_me(vrw_pos,2);
  vus_sme(ii) = sim_eq_me(vus_pos,2);
end
for ii=1:NN
  sim_eq_nof = simult_(M_nof_alt,options_nof_alt,sim_eq_me_ergo(:,ii),oo_nof_alt.dr,shock_matrix',options_nof_alt.order);
  vrw_me_nof_sim(ii) = sim_eq_nof(vrw_pos,2);
  vus_me_nof_sim(ii) = sim_eq_nof(vus_pos,2);
end

upsilon_us_transition = mean(((vus_me_nof_sim./vus_sme).^(1/(1-ggamma)) - 1)*100)
upsilon_rw_transition = mean(((vrw_me_nof_sim./vrw_sme).^(1/(1-ggamma)) - 1)*100)

Gamma_nobenefit_us_alt = upsilon_us_transition;
Gamma_nobenefit_rw_alt = upsilon_rw_transition;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

save nof_alt_save oo_nof_alt M_nof_alt options_nof_alt Gamma_nobenefit_us_alt Gamma_nobenefit_rw_alt;
